﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OpenFuse.Cache
{
 	[Serializable]
	public class Account : MarshalByRefObject, Comms.IAccount
    {
		public virtual int RowId { get; set; }
        public virtual DateTime CreationTime { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Password { get; set; }
        public virtual Comms.IGrid Grid { get; set; }
        public virtual bool IsActive { get; set; }
		
		// Commands
		public virtual void Login()
		{
			Backend.SlSession session = new Backend.SlSession(Management.Management.Cache, Grid.Nick, FirstName, LastName);
			Management.Management.Sessions.Add(session);
			if(session.Login(Password, Grid.LoginUrl))
			{
				Console.WriteLine("login succeeded");
			}
			else
			{
				Console.WriteLine("login failed");
			}
		}
		
		public virtual void Logout()
		{
			IList<Backend.SlSession> sessions = Management.Management.Sessions;
			
			for(int i = 0; i < sessions.Count; i++)
			{
				if(sessions[i].FirstName == FirstName && sessions[i].LastName == LastName && sessions[i].GridName == Grid.Nick)
				{
					sessions[i].Dispose();
					sessions.RemoveAt(i);
					break;
				}
			}
		}
		
		public void Replicate()
		{
			IList<Backend.SlSession> sessions = Management.Management.Sessions;
			Console.WriteLine("Replicating");
			for(int i = 0; i < sessions.Count; i++)
				if(sessions[i].FirstName == FirstName && sessions[i].LastName == LastName && sessions[i].GridName == Grid.Nick)
				{
					sessions[i].StartReplication();
					break;
				}
		}

    }
}
